package com.david.viewtest.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

class BadgeDotView(
    context: Context,
    attrs: AttributeSet,
) : View(context, attrs) {
    /**
     * 红点半径
     */
    private var radius = 0f
    private var paint: Paint = Paint()

    init {
        //val a = context.obtainStyledAttributes(attrs, R.styleable.zeekr_badge)
        paint.color = Color.RED
        paint.textSize = 40f
        //a.recycle()
        paint.style = Paint.Style.FILL
        paint.strokeWidth = 3f
        paint.isAntiAlias = true
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        radius = if (measuredHeight > measuredWidth) (measuredWidth / 2).toFloat() else (measuredHeight / 2).toFloat()
    }

    override fun onDraw(canvas: Canvas) {
        paint.style = Paint.Style.STROKE
        canvas?.drawCircle((width / 2).toFloat(), (height / 2).toFloat(), radius - 3, paint)
        val titleStartY = (height / 2f) - (paint.descent() + paint.ascent()) / 2f - 2

        paint.style = Paint.Style.FILL
        paint.color = Color.WHITE
        canvas?.drawText("1",  width / 2 - paint.measureText("1") / 2, titleStartY, paint)

    }
}